<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * Mall
 */
namespace Mall\Controller;
use Common\Controller\AdminController;

/**
 * 商品管理
 */
class OrderNodeController extends AdminController{
	
	/**
	 * 文章管理
	 */
	 
	 public function getMyExpressNode(){
		
		
	
		$m_mall_content = M('mall_content');
		
		$map['type'] = get_mall_model_conf('MALL_MY_EXPRESS_NODE');
		$map['status'] = 1;
		
		$list = $m_mall_content->where($map)->select();
		
		return $list;
	}
	
    public function Manage(){

		
		common_public()->assign_page('自有物流');
		
		$m_mall_content = M('mall_content');
		$m_mall_order = M('mall_order');
		$m_common_user = M('common_user');
		$m_mall_order_node = M('mall_order_node');
		
		$onmap['status'] = 2;
		$onmap['uid'] = getUID();
	
		$list = $m_mall_order_node->where($onmap)->order('time desc')->select();
		
		foreach($list as $li=>$lk){
			
			$nmap['id'] = $list[$li]['now_status'];
			$ndata = $m_mall_content->where($nmap)->find();
			$list[$li]['now_status_name'] = $ndata['name'];
			
			$omap['order_id'] = $list[$li]['order_id'];
			$omap['form_order_id'] = 0;
			$odata = $m_mall_order->where($omap)->find();
			
			$osnmap['id'] = $odata['now_status'];
			$ondata = $m_mall_content->where($osnmap)->find();
		
			$list[$li]['begin_status_name'] = $ondata['name'];
		}
	
		$this->assign('list',$list);
		
		
		$node = $this->getNode();
	
		$id = I('param.id');
		
		foreach($node as $i=>$k){
			
			if($node[$i]['id']==$id){
				
				$node[$i]['active'] = 'class="active"';
			}
			
		}
		$this->assign('node',$node);
		$this->assign('nodes',$node);
		$this->assign('nodess',$node);
		$this->assign('nodesss',$node);
		$this->assign('now_status',$id);
		
		
		$this->display();
	}
	/**
	 * 获得分类
	 */
	public function getChannel(){
		
			$m_mall_content = M('mall_content');
		
			//获得顶级分类
			$map['form_id'] = 0;
			$map['type'] = get_mall_model_conf('MALL_CLASSIFY');
			$map['status'] = 1;
			
			$list = $m_mall_content->where($map)->order('sort desc')->select();
			
			//获得下级分类即商品数量
			foreach($list as $i=>$k){
				
				$down_map['form_id'] = $list[$i]['id'];
				$down_map['type'] = get_mall_model_conf('MALL_CLASSIFY');
				$down_map['status']  =1;
				$down_list = $m_mall_content->where($down_map)->order('sort desc')->select();
				
				$list[$i]['down'] = $down_list;
				
			}
		
	
			return $list;
	}
	
	/**
	 * 新增文章
	 */
	public function getNode(){
		

		$m_mall_content = M('mall_content');
		
		
		$map['type'] = get_mall_model_conf('MALL_MY_EXPRESS_NODE');
		$map['status'] = 1;
	
		$list = $m_mall_content->where($map)->order('sort desc')->select();
	
		return $list;
		
	
	}

	
	/**
	 * 新增文章
	 */
	public function Change(){
		
		if(IS_AJAX){

			$m_mall_order_node = M('mall_order_node');
			$m_mall_order = M('mall_order');
			
			
			$data['order_id'] = I('param.order_id');
			$data['now_status'] = I('param.now_status');
			
			if($data['now_status']==142){
				
				$omap['order_id'] = $data['order_id'];
				$omap['form_order_id'] = 0;
				$omap['status'] = 4;
				$odata = $m_mall_order->where($omap)->find();
				
				if($odata){
					
					$data['is_change'] = 1;
					
				}else{
					
					$data['is_change'] = 2;
					
				}
			}else if($data['now_status']==143){
				
				$omap['order_id'] = $data['order_id'];
				$omap['form_order_id'] = 0;
				$omap['status'] = 5;
				$odata = $m_mall_order->where($omap)->find();
				
				if($odata){
					
					$data['is_change'] = 1;
					
				}else{
					
					$data['is_change'] = 2;
					
				}
				
			}else{
				
				$data['is_change'] = 1;
			}
			
			$data['uid'] = getUID();
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 2;
		
			$m_mall_order_node->add($data);
	
		}
	
	}
	
	/**
	 * 批量改变状态
	 */
	public function Delete($id,$status=0){
		
	
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_mall_order_node = M('mall_order_node');
			
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			
			if($id){
			
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$map['id'] = $id[$i];
					$data['status'] = $status; //改成5已发货
					
					$m_mall_order_node->where($map)->save($data);
				
				}
			
				//返回信息
				$info = get_errcode(0);
		
			}else{
				
				$info = get_errcode(10000001);
			}
				
		
			
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
	/**
	 * 批量改变状态
	 */
	public function ChangeInfo($id){
		
	
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_mall_order_node = M('mall_order_node');
			
			$id = $id?$id:I('param.id');
			//$status = $status?$status:I('param.status');
			
			
			if($id){
			
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$map['id'] = $id[$i];
					$map['is_change'] = 1; //改成5已发货
					
					$data = $m_mall_order_node->where($map)->find();
					
					//变更
					if($data){

						$this->NowStatus($data['order_id'],$data['now_status']);
						
						$c_data['status'] = 1;
						$m_mall_order_node->where($map)->save($c_data);
					}
					
					
					
					
					
					
				
				}
			
				//返回信息
				$info = get_errcode(0);
		
			}else{
				
				$info = get_errcode(10000001);
			}
				
		
			
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	/**
	 * 批量改变状态
	 */
	public function NowStatus($order_id,$now_status){
		
			$m_common_user = M('common_user');
	
			$m_mall_content = M('mall_content');
			$m_mall_order = M('mall_order');
			$m_mall_money = M('mall_money');
				
				
		
			$order_map['order_id'] = $order_id;
			$order_map['form_order_id'] = 0;
			
			$order_data = $m_mall_order->where($order_map)->find();
			
			if(!$order_data){
				
				die;
			}
			
			$status = $now_status;
			
			$imap['id'] = $status;
			$idata = $m_mall_content->where($imap)->find();
				
			//改变成已发货
			if($status==142){
				
				
					
				$map['id'] = $order_data['id'];
				$map['status'] = 4; //状态是4才能变成5
				$data['now_status'] = $status;
				$data['status'] = 5; //改成5已发货
				
				$result = $m_mall_order->where($map)->save($data);
				
				if($result){
					
					//获得物流信息
					$smap['id'] = $order_data['id'];
					$order = $m_mall_order->where($smap)->find();
					
					
					$odata['value'] = $idata['value'];
						
					
					
					$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
						
					$odata['uid'] = getUID();
				
					$odata['name'] = $order['order_id'];
					
					$odata['time'] = time();
					$odata['ip'] = get_client_ip();
					$odata['status'] = 1;
					
					$m_mall_content->add($odata);
				}
					
			
			
				
			//改变为已完成 TODO涉及财务和积分问题暂缓写
			}else if($status==143){
				
				
			
					
					
					$map['id'] = $order_data['id'];
					$map['status'] = 5; //状态是5才能变成6
					$data['now_status'] = $status;
					$data['status'] = 6; //改成6已完成
					
					$result = $m_mall_order->where($map)->save($data);
					
					if($result){
						
						//获得物流信息
						$smap['id'] = $order_data['id'];
						$order = $m_mall_order->where($smap)->find();
						
						
						$odata['value'] = $idata['value'];
							
						
						
						$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
							
						$odata['uid'] = getUID();
					
						$odata['name'] = $order['order_id'];
						
						$odata['time'] = time();
						$odata['ip'] = get_client_ip();
						$odata['status'] = 1;
						
						$m_mall_content->add($odata);
						
						
						//财务信息
						//积分奖励部分
						$m_mall_integral = M('mall_integral');
				
						//创建奖励信息
						$mdata['uid'] = $order['uid'];
						$mdata['type'] = get_mall_order_conf('MALL_INTEGRAL_TYPE_1');
						$mdata['type_value'] = get_mall_order_conf('MALL_INTEGRAL_TYPE_1_1');
						$mdata['begin_integral'] = getUIDIntegral($order['uid']);
						$mdata['integral'] = $order['goods_integral'];
						$mdata['end_integral'] = getUIDIntegral($order['uid'])+$order['goods_integral'];
						$mdata['value'] = '订单号'.$order['order_id'].'奖励积分';
						$mdata['time'] = time();
						$mdata['ip'] = get_client_ip();
						$mdata['status'] = 1;
						
						
						$m_mall_integral->add($mdata);
						
						//增加积分
						$umap['id'] = $order['uid'];;
						
						
						$m_common_user->where($umap)->setInc('integral',$order['goods_integral']); //增加积分
						
						//佣金部分
						//判断是否有合伙人
						if($order['partner_id']!=1){
							
							//反本金 判断是不是货到付款
							if($order['pay_type']==4){
								//创建财务信息
							
								$mbsdata['uid'] = $order['partner_id'];
								$mbsdata['type'] = get_mall_order_conf('MALL_MONEY_TYPE_1');
								$mbsdata['type_value'] = get_mall_order_conf('MALL_MONEY_TYPE_1_4');
								$mbsdata['begin_money'] = getUserIDMoney($order['partner_id']);
								$mbsdata['money'] = $order['money'];
								$mbsdata['end_money'] = getUserIDMoney($order['partner_id'])+$order['money'];
								$mbsdata['value'] = '订单号'.$order['order_id'].'本金返还';
								$mbsdata['time'] = time();
								$mbsdata['ip'] = get_client_ip();
								$mbsdata['status'] = 1;
								
								$m_mall_money->add($mbsdata);
								
								$mbsmap['id'] = $order['partner_id'];
								$m_common_user->where($mbsmap)->setInc('money',$order['money']); //增加金额
						
							
							}
							
							//反佣金
							
							//创建财务信息
							$msdata['uid'] = $order['partner_id'];
							$msdata['type'] = get_mall_order_conf('MALL_MONEY_TYPE_1');
							$msdata['type_value'] = get_mall_order_conf('MALL_MONEY_TYPE_1_3');
							$msdata['begin_money'] = getUserIDMoney($order['partner_id']);
							$msdata['money'] = $order['goods_partner_money'];
							$msdata['end_money'] = getUserIDMoney($order['partner_id'])+$order['goods_partner_money'];
							$msdata['value'] = '订单号'.$order['order_id'].'佣金奖励';
							$msdata['time'] = time();
							$msdata['ip'] = get_client_ip();
							$msdata['status'] = 1;
							
							
							$m_mall_money->add($msdata);
							
							$msmap['id'] = $order['partner_id'];
							$m_common_user->where($msmap)->setInc('money',$order['goods_partner_money']); //增加金额
							
							
							
						}
						
						
						
					}
					
				
				
			
				
			}else{
				
				
					
					
					$map['id'] =  $order_data['id'];
					$data['now_status'] = $status;
					
					$m_mall_order->where($map)->save($data);
					
					//获得物流信息
					$order = $m_mall_order->where($map)->find();
					
					
					$odata['value'] = $idata['value'];
						
					
					
					$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
						
					$odata['uid'] = getUID();
				
					$odata['name'] = $order['order_id'];
					
					$odata['time'] = time();
					$odata['ip'] = get_client_ip();
					$odata['status'] = 1;
					
					$m_mall_content->add($odata);
				
			}
			
			
	
		
	

	}
	
	
}